package per.zqx.simplest;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Consumer
 *
 * @author Sa  2021-03-16 14:17
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 队列名称
        String QUEUE_NAME = "simple_queue";
        // 连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 设置主机
        connectionFactory.setHost("localhost");
        // 创建连接
        Connection connection = connectionFactory.newConnection();
        // 创建通道
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        // 回调方法
        DeliverCallback deliverCallback = (consumerTag,delivery) -> {
            String msg = new String(delivery.getBody(),"utf-8");
            System.out.println("Consumer received a msg : " + msg);
        };
        // 消费
        boolean autoAck = true;
        channel.basicConsume(QUEUE_NAME,autoAck, deliverCallback,consumerTag -> {
            System.out.println(consumerTag);
        });
    }
}
